package dcedit.util;

// Import Java SDK Classes
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/** DCTextField class */
public class DCTextField extends JTextField {
   /** Serializable constant */
   public static final long serialVersionUID = 1289514748763L;

   /** key adapter field */
   private KeyAdapter KEY_ADAPTER = new KeyAdapter() {
      public void keyReleased(KeyEvent ke) {
         final DCTextField FIELD = (DCTextField)ke.getSource();
         final String TEXT = FIELD.getText();
         final int LIMIT = FIELD.getTextLimit();
         if(TEXT.length() > LIMIT) {
            FIELD.setText(TEXT.substring(0,LIMIT));
         } // END if
      } // END keyReleased
   };

   /** enum for field content type */
   public static enum CONTENT_TYPE {
      /** Specifies that this text field may contain any text. */
      ALL_TEXT,
      /** Specifies that this text field may contain any printable text, no whitespace characters. */
      NO_WHITESPACE,
      /** Specifies that this text field may contain only letter and number characters.  No punctuation characters, no whitespace characters. */
      ALPHA_NUMERIC_ONLY,
      /** Specifies that this text field may contain only letter characters. */
      ALPHA_ONLY,
      /** Specifies that this text field may contain only number characters. */
      NUMERIC_ONLY;
   };

   /** The label which is associated with this text field */
   private JLabel label = new JLabel();

   /** Text character limit field */
   private int textLimit = Integer.MAX_VALUE;

   /** Content type field */
   private CONTENT_TYPE contentType = CONTENT_TYPE.ALL_TEXT;

   /** constructor */
   public DCTextField() {
      this(null, null);
   } // END constructor

   /** constructor */
   public DCTextField(int columns) {
      this(columns, null, null);
   } // END constructor

   /** constructor */
   public DCTextField(String label) {
      this(label, null, null);
   } // END constructor

   /** constructor */
   public DCTextField(String label, int columns) {
      this(label, columns, null, null);
   } // END constructor

   /** constructor */
   public DCTextField(Color foregroundColor, Color backgroundColor) {
      super();

      if(null != foregroundColor && null != backgroundColor) {
         this.setForeground(foregroundColor);
         this.setBackground(backgroundColor);
         this.setDisabledTextColor(foregroundColor);
         this.setBorder(BorderFactory.createLineBorder(foregroundColor));
      } // END if

      this.addKeyListener(KEY_ADAPTER);
   } // END constructor

   /** constructor */
   public DCTextField(int columns, Color foregroundColor, Color backgroundColor) {
      super(columns);

      if(null != foregroundColor && null != backgroundColor) {
         this.setForeground(foregroundColor);
         this.setBackground(backgroundColor);
         this.setDisabledTextColor(foregroundColor);
         this.setBorder(BorderFactory.createLineBorder(foregroundColor));
      } // END if

      this.addKeyListener(KEY_ADAPTER);
   } // END constructor

   /** constructor */
   public DCTextField(String label, Color foregroundColor, Color backgroundColor) {
      super();

      this.setLabel(label);

      if(null != foregroundColor && null != backgroundColor) {
         this.setForeground(foregroundColor);
         this.setBackground(backgroundColor);
         this.setDisabledTextColor(foregroundColor);
         this.setBorder(BorderFactory.createLineBorder(foregroundColor));
      } // END if

      this.addKeyListener(KEY_ADAPTER);
   } // END constructor

   /** constructor */
   public DCTextField(String label, int columns, Color foregroundColor, Color backgroundColor) {
      super(columns);

      this.setLabel(label);

      if(null != foregroundColor && null != backgroundColor) {
         this.setForeground(foregroundColor);
         this.setBackground(backgroundColor);
         this.setDisabledTextColor(foregroundColor);
         this.setBorder(BorderFactory.createLineBorder(foregroundColor));
      } // END if

      this.addKeyListener(KEY_ADAPTER);
   } // END constructor

   /** setTextLimit method */
   public void setTextLimit(int textLimit) {
      if(textLimit > 0) {
         this.textLimit = textLimit;
         final String TEXT = this.getText();
         final int LIMIT = this.getTextLimit();
         if(TEXT.length() > LIMIT) {
            this.setText(TEXT.substring(0,LIMIT));
         } // END if
      } //END if
   } // END setTextLimit

   /** getLabel method */
   public JLabel getLabel() {
      return label;
   } // END getLabel

   /** setLabel method */
   public void setLabel(String label) {
      if(null != label) {
         this.label = new JLabel(label);
         this.label.setForeground(this.getForeground());
         this.label.setBackground(this.getBackground());
      } // END if
   } // END setLabel

   /** getTextLimit method */
   public int getTextLimit() {
      return this.textLimit;
   } // END getTextLimit

   /** setContentType method */
   public void setConentType(CONTENT_TYPE contentType) {
      this.contentType = contentType;
   } // END setContentType

   /** getContentType method */
   public CONTENT_TYPE getContentType() {
      return this.contentType;
   } // END getContentType

   /** isEmpty method */
   public boolean isEmpty() {
      return this.getText().isEmpty();
   } // END isEmpty
} // END class DCTextField